package com.lyhengtongwl.zqsnews.ui.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lyhengtongwl.zqsnews.App;
import com.lyhengtongwl.zqsnews.R;
import com.lyhengtongwl.zqsnews.adapter.NewsNoticeAdapter;
import com.lyhengtongwl.zqsnews.base.BaseActivity;
import com.lyhengtongwl.zqsnews.entity.NewsBaseEntity;
import com.lyhengtongwl.zqsnews.entity.NewsNoticeEntity;
import com.lyhengtongwl.zqsnews.manage.DBManager;
import com.lyhengtongwl.zqsnews.net.MyCallback;
import com.lyhengtongwl.zqsnews.net.Task;
import com.lyhengtongwl.zqsnews.utils.AppConfig;
import com.lyhengtongwl.zqsnews.utils.TitleBuilder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Response;

public class NewsNoticeActivity extends BaseActivity {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private NewsNoticeAdapter newsAdapter;
    private List<NewsNoticeEntity> list = new ArrayList<>();

    @Override
    public void setListen() {

    }

    @Override
    public void setTitleBarColor() {
        new TitleBuilder(this).setTitleText("系统消息")
                .setLeftIcoListening(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_notice;
    }

    @Override
    public void initView() {
        if (DBManager.getAllNews().size() > 0) {
            Log.e("TAG", "initView: ");
        } else {
            getGG();
        }
    }

    @Override
    public void initData() {

    }

    //获取公告列表
    private void getGG() {
        Task.getApiService().getGG().enqueue(new MyCallback<NewsBaseEntity>(NewsNoticeActivity.this) {
            @Override
            protected void onSuccess(Response<NewsBaseEntity> response) {
                try {
                    if ("1".equals(response.body().getCode())) {
                        String jsonStr = JSONObject.toJSONString(response.body().getData());
                        JSONArray jsonArray = JSONArray.parseArray(jsonStr);
                        list.clear();
                        for (int i = 0; i < jsonArray.size(); i++) {
                            JSONObject jsonObject = JSON.parseObject(jsonArray.get(i).toString());
                            NewsNoticeEntity newsGG = new NewsNoticeEntity();
                            newsGG.setLinkUrl(jsonObject.getInteger("id") + "");
                            newsGG.setContent(jsonObject.getString("content"));
                            newsGG.setTitle(jsonObject.getString("title"));
                            newsGG.setTime(jsonObject.getString("remark"));
                            list.add(newsGG);
                        }
                        LinearLayoutManager manager = new LinearLayoutManager(NewsNoticeActivity.this);
                        newsAdapter = new NewsNoticeAdapter(NewsNoticeActivity.this, list);
                        recyclerView.setLayoutManager(manager);
                        recyclerView.setAdapter(newsAdapter);
                        newsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                startActivity(new Intent(NewsNoticeActivity.this, NewsWebCommonActivity.class).putExtra("url", AppConfig.getInstance(App.getContext()).get("newUrl")+"?id="+list.get(position).getLinkUrl()));
                            }
                        });
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            protected void onFailure(Throwable t) {

            }
        });
    }
}
